<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class LibConfig {
	
	//constructor
	function LibConfig($config) {
		if( is_array($config) ) {
			$this->loadFromArray($config);
		} elseif( strstr($config, '<?xml') !== false ) {
			$this->loadFromXML($config);
		} elseif( strstr($config, '.xml') !== false ) {
			$this->loadFromXMLFile($config);
		} elseif( strstr($config, '.ini') !== false ) {
			$this->loadFromINIFile($config);
		}
	}
	
	function init() {
		$this->_FILE_PATH = CONFIG_PATH . CONFIG_DIR_INCLUDES . 'config/';
	}
	
	//methods
	function loadFromArray($config, $obj = NULL) {
		if( $obj == NULL ) {
			$obj = $this;
		}
		foreach($config as $k => $v) {
			if( is_array($v) ) {
				$this->$k = new LibConfig($v, $obj);
			} else {
				$this->$k = $v;
			}
		}
		
		return $obj;
	}
	
	function loadFromArrayFile($xml_file) {
		$c = $this->loadFromFile($xml_file);
	}
	
	function loadFromXML($xml) {
		$c = $this->loadFromFile($xml_file);
	}
	
	function loadFromXMLFile($xml_file) {
		$c = $this->loadFromFile($xml_file);
	}
	
	function loadFromINIFile($ini_file) {
		$c = $this->loadFromFile($xml_file);
	}
	
	//load
	function loadFromFile($filename) {
		$filename = $this->_FILE_PATH . $filename;
		if( is_file($filename) ) {
			return file_get_contents($filename);
		}
		
		return '';
	}
	
	//array to xml
	function arraytoxml($array, $f = true) {
		$xml = '';
		if( $f == true ) {
			$xml .= '<?xml version="1.0" encoding="utf-8"?>';
		}
		foreach($array as $k => $v) {
			$xml .= '<' . $k . '>';
			if( is_array($v) ) {
				$xml .= $this->arraytoxml($v, false);
			} else {
				$xml .= $v;
			}
			$xml .= '</' . $k . '>';
		}
		
		return $xml;
	}
	
	//xml to array
	function xmltoarray($xml) {
		
	}
}
//
?>